﻿function handleError(msg){
    art.tip.danger(msg);
    //alert(msg)
}

function fileDialogComplete()
{
    var swfu = this;

    if (typeof (jcrop_api) != "undefined") {
        jcrop_api.destroy();
    }
    swfu.startUpload();            
}

function fileQueueError(file, errorCode, message)
{
    var swfu = this;

    try
    {
        if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED)
        {
            handleError("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
            return;
        }

        switch (errorCode)
        {
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                //progress.setStatus("File is too big.");
                handleError("文件" + file.name + "大小超过限制!");
                this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                //progress.setStatus("Cannot upload Zero Byte files.");
                handleError("不能上传0节字文件" + file.name + "!");
                this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                //progress.setStatus("Invalid File Type.");
                handleError("文件" + file.name + "属于不允许上传的类型!");
                this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
            default:
                handleError("文件" + file.name + "出现未知错误!");
                this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
        }
    } catch (ex)
    {
        this.debug(ex);
    }

    swfu.setButtonDisabled(false);
}

function uploadStart(file)
{
    var swfu = this;
    try
    {
        $("#upload_image .progress .progress-bar").width(0);
        $("#upload_image .progress").show();
        //$("#con").text(sMsgInfo_UploadProgress.replace("{0}", 0));
    }
    catch (ex) { }

    return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal)
{
    var swfu = this;
    try
    {
        var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);        
        
        $("#upload_image .progress .progress-bar").width(percent);
        //$("#con").text(sMsgInfo_UploadProgress.replace("{0}", parseInt(percent).toString()));
        
        swfu.setButtonDisabled(true);

    } catch (ex)
    {
        this.debug(ex);
    }
}

function uploadSuccess(file, serverData)
{
    var swfu = this;
    swfu.setButtonDisabled(false);
    $("#upload_image .progress").hide();
    try
    {
        if(typeof swfu.settings.custom_settings.upload_success_handler == "function"){
            swfu.settings.custom_settings.upload_success_handler(file, serverData);
        }
        //jcrop

    } catch (ex)
    {
        this.debug(ex);
    }
}

function uploadComplete(file)
{
    var swfu = this;
    try{
        //swf.stratUpload();        
    }
    catch (ex){
            this.debug(ex);
    }
}

function uploadError(file, errorCode, message)
{
    var swfu = this;
    try
    {
        switch (errorCode)
        {
            case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                //progress.setStatus("Upload Error: " + message);
                handleError("Upload Error:" + message);
                this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
                //progress.setStatus("Upload Failed.");
                handleError("上传失败!");
                this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
            case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                //progress.setStatus("Server (IO) Error");
                handleError("服务器IO错误!");
                this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
                break;
            case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                //progress.setStatus("Security Error");
                handleError("服务器安装错误!");
                this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                //progress.setStatus("Upload limit exceeded.");
                handleError("上传被限制执行!");
                this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
            case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
                //progress.setStatus("Failed Validation.  Upload skipped.");
                handleError("文件无效,跳过该文件!");
                this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                //progress.setStatus("Cancelled");
                //handleError("上传被终止!");
                //progress.setCancelled();
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                //progress.setStatus("Stopped");
                //handleError("上传被停止!");
                break;
            default:
                //progress.setStatus("Unhandled Error: " + errorCode);
                handleError("未知异常,ErrorCode:" + errorCode);
                this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                break;
        }
    } catch (ex)
    {
        this.debug(ex);
    }

    swfu.cancelUpload();
    swfu.setButtonDisabled(false);
}

// This event comes from the Queue Plugin
function queueComplete(numFilesUploaded)
{
    /*var status = document.getElementById("divStatus");
    status.innerHTML = numFilesUploaded + " file" + (numFilesUploaded === 1 ? "" : "s") + " uploaded.";*/
}